#!/bin/bash -e

if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
    echo "Illegal number of parameters"
    exit 1
fi

TAG=$1
DRY_RUN=""
[ "$#" -eq 2 ] && DRY_RUN=$2

BASE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd $BASE

echo "---- Build Discuz Q and Commit to Git Repo -----------------------------"

[ "$DRY_RUN" = "-n" ] && echo "Dry run, no changes will be pushed to remote"

# DiscuzQ后端代码 仓库
DISCUZ_REPO="git@gitee.com:Discuz/Discuz-Q.git"
# DiscuzQ前台代码 编译后的静态资源仓库
DISCUZ_WEB="git@gitee.com:kotlindev/dzq-default-web-static.git"
# DiscuzQ后台代码 编译后的静态资源仓库
DISCUZ_ADMIN="git@gitee.com:kotlindev/dzq-admin-static.git"
# 构建的之后的仓库
DISCUZ_BUILDED="git@gitee.com:Discuz/discuz-q-release.git"



function fix_composer {
    cd $BASE/dz_temp
    echo "---- fixing composer.json"
    cat composer.json | jq ".require.\"discuz/core\" = \"$TAG\"" | jq '.repositories = [{ "type": "composer", "url": "https://cloud.discuz.chat" }]' > composer-fixed.json
    mv composer-fixed.json composer.json
}

function mkdir_public {
    cd $BASE/dz_temp
    echo "---- mkdir public_1"
    mkdir public_1
    cd $BASE/dz_temp/public_1
    echo 1 > skin.conf

    echo "---- mkdir public_2"
    cd $BASE/dz_temp
    mkdir public_2
    cd $BASE/dz_temp/public_2
    echo 2 > skin.conf
}

function build_admin {
    cd $BASE/dz_temp
    echo "---- building admin"
    docker run --rm -i -v $PWD:/usr/src/app node:12 /bin/bash -c "cd /usr/src/app/resources/frame && npm install && npm run-script build-admin"
    if [ $? -eq 0 ]; then
        echo "---- npm build successfully"
        echo "---- removing resources/frame/node_modules"
        rm -rf resources/frame/node_modules
    else
        echo "---- npm build failed"
        exit 1
    fi
    cp $BASE/dz_temp/public/admin.html $BASE/dz_temp/public_1/
    cp -r $BASE/dz_temp/public/static-admin $BASE/dz_temp/public_1/
}

function build_admin_pay {
    cd $BASE/dz_temp
    echo "---- building admin pay"
    docker run --rm -i -v $PWD:/usr/src/app node:12 /bin/bash -c "cd /usr/src/app/resources/frame && npm install && npm run-script build-admin:pay"
    if [ $? -eq 0 ]; then
        echo "---- npm build successfully"
        echo "---- removing resources/frame/node_modules"
        rm -rf resources/frame/node_modules
    else
        echo "---- npm build failed"
        exit 1
    fi
    cp -r $BASE/dz_temp/public_pay/* $BASE/dz_temp/public_2
    rm -rf $BASE/dz_temp/public_pay
}

function commit_all {
    cd $BASE/dz_temp
    echo "---- commit build result"
    sed -i '/\/public\/static/d' .gitignore
    sed -i '/\/public\/index\.html/d' .gitignore
    sed -i '/\/public\/admin\.html/d' .gitignore
    sed -i '/\/app\/Console\/Commands\/Upgrades\//d'  .gitignore

    rm -f app/Console/Commands/Upgrades/.gitignore
    rm -rf .github
    git add --all
    git commit -m "auto vue build by bot"
}

function retag_repo {
    cd $BASE/dz_temp
    echo "---- retag the repo"
    git tag -d $TAG
    git tag $TAG
}

function push_to_remote {
    cd $BASE/dz_temp
    if [ ! "$DRY_RUN" = "-n" ]; then
        echo "---- push to build repo"
        #git remote add build git@git.code.oa.com:discuzq-build2/Discuz.git
        git remote add build $DISCUZ_BUILDED
        # push code
        git push build master -f
        # push tag
        git push build $TAG -f
    else
        echo "---- Dry run mode, do not actually push"
    fi
}

function checkout_uniapp {
    cd $BASE
    echo "---- removing uniapp_temp"
    rm -rf $BASE/uniapp_temp

    echo "---- clone new copy of uniapp to uniapp_temp"
    git clone $UNIAPP_REPO uniapp_temp
}

function checkout_discuz {
    cd $BASE
    echo "---- removing dz_temp"
    rm -rf $BASE/dz_temp

    echo "---- clone new copy of Discuz to dz_temp"
    git clone $DISCUZ_REPO dz_temp
}


function checkout_tag_as_branch {
    cd $1
    echo "---- checking out tag $TAG as branch br_$TAG under $1"
    git checkout -b br_$TAG $TAG
}

function build_uniapp {
    cd $BASE/uniapp_temp
    echo "---- building uniapp"
    docker run --rm -it -v $PWD:/usr/src/app node:12 /bin/bash -c "cd /usr/src/app && npm --no-update-notifier install && npm --no-update-notifier run-script build:h5"
}

function build_web {
    cd $BASE/web_temp
    echo "---- building web"
    docker run --rm -it -v $PWD:/usr/src/app node:12 /bin/bash -c "cd /usr/src/app && npm --no-update-notifier install && npm --no-update-notifier run generate"
}

function checkout_web_build {
    cd $BASE
    echo "---- removing web_temp_builded"
    rm -rf $BASE/web_temp_builded

    echo "---- clone new copy of discuz-dist-pc to web_temp_builded"
    git clone $DISCUZ_WEB web_temp_builded

}
function checkout_web_pay_build {
    cd $BASE
    echo "---- removing web_pay_temp_builded"
    rm -rf $BASE/web_pay_temp_builded

    echo "---- clone new copy of discuz-dist-pc-pay to web_pay_temp_builded"
    git clone $DISCUZ_DIST_PC_PLAY web_pay_temp_builded

}

function checkout_uniapp_build {
  cd $BASE
  echo "---- removing uniapp_temp_builded"
  rm -rf $BASE/uniapp_temp_builded

  echo "---- clone new copy of discuz-dist-h5 to uniapp_temp_builded"
  git clone $DISCUZ_DIST_UNIAPP uniapp_temp_builded
}

function checkout_uniapp_pay_build {
  cd $BASE
  echo "---- removing uniapp_pay_temp_builded"
  rm -rf $BASE/uniapp_pay_temp_builded

  echo "---- clone new copy of discuz-dist-h5 to uniapp_pay_temp_builded"
  git clone $DISCUZ_DIST_UNIAPP_PLAY uniapp_pay_temp_builded
}




function checkout_admin_build {
  cd $BASE
  echo "---- removing web_admin_temp_builded"
  rm -rf $BASE/web_admin_temp_builded

  echo "---- clone new copy of discuz-dist-h5 to web_admin_temp_builded"
  git clone $DISCUZ_ADMIN  web_admin_temp_builded
}



function copy_uniapp_to_discuz {
    echo "---- copying uniapp h5 result to discuz folder"
    cd $BASE/uniapp_temp_builded

    rm -rf .git .github
    cp -r * $BASE/dz_temp/public
    cp -r * $BASE/dz_temp/public_1
}

function copy_uniapp_pay_to_discuz {
    echo "---- copying uniapp h5 pay result to discuz folder"
    cd $BASE/uniapp_pay_temp_builded

    rm -rf .git .github
    cp -r * $BASE/dz_temp/public_2
}

function copy_web_to_discuz {
    echo "---- copying web result to discuz folder"
    cd $BASE/web_temp_builded

    rm -rf .git .github
    cp -r * $BASE/dz_temp/public/

}
function copy_web_pay_to_discuz {
    echo "---- copying web pay result to discuz folder"
    cd $BASE/web_pay_temp_builded

    rm -rf .git .github
    cp -r * $BASE/dz_temp/public_2/

}

function copy_admin_to_discuz {
    echo "---- copying admin result to discuz folder"
    cd $BASE/web_admin_temp_builded
    rm -rf .git .github
    cp -r * $BASE/dz_temp/public/
}

function copy_upgrade_commands {
    cd $BASE
    echo "---- copying upgrade commands"
    if [ ! -d $BASE"/dz_temp/app/Console/Commands/Upgrades/" ];
        then
        mkdir $BASE/dz_temp/app/Console/Commands/Upgrades/
    fi
    cp upgrade-commands/* dz_temp/app/Console/Commands/Upgrades/

}




checkout_web_build
checkout_tag_as_branch $BASE/web_temp_builded

checkout_admin_build
checkout_tag_as_branch $BASE/web_admin_temp_builded

checkout_discuz
checkout_tag_as_branch $BASE/dz_temp

copy_upgrade_commands

copy_web_to_discuz
copy_admin_to_discuz

commit_all
retag_repo
push_to_remote
